package cn.tedu.charging.device.service.impl;


import cn.tedu.charging.common.pojo.param.GunStatusUpdateParam;
import cn.tedu.charging.common.pojo.vo.StationInfoVO;
import cn.tedu.charging.device.dao.repository.GEOTemplateContext;
import cn.tedu.charging.device.dao.repository.StationCacheTemplateRepository;
import cn.tedu.charging.device.pojo.po.StationInfoWrapper;
import cn.tedu.charging.device.service.DeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class DeviceServiceImpl implements DeviceService {


    @Override
    public StationInfoVO getStationInfo(Integer gunId) {
        StationInfoVO stationInfoVO=new StationInfoVO();
        stationInfoVO.setStationId(1);
        return stationInfoVO;
    }

    @Override
    public Boolean updateGunStatus(GunStatusUpdateParam gunStatusUpdateParam) {
        return true;
    }

    @Override
    public List<StationInfoVO> getNearStation(Double longitude, Double latitude, Double distance) {
        log.debug("获取附近充电站:{},{},{}", longitude, latitude, distance);
        StationCacheTemplateRepository repository = getCacheTemplateRepository();
        List<StationInfoWrapper> nearStation = repository.search(longitude, latitude, distance);
        List<StationInfoVO> stationInfoVOList = new ArrayList<>();
        for (StationInfoWrapper stationInfo : nearStation) {
            StationInfoVO stationInfoVO = new StationInfoVO();
            stationInfoVO.setStationId(stationInfo.getStationPO().getId());
            stationInfoVO.setStationName(stationInfo.getStationPO().getStationName());
            stationInfoVO.setStationLat(stationInfo.getStationPO().getStationLat());
            stationInfoVO.setStationLng(stationInfo.getStationPO().getStationLng());

            //设置动态的距离
            stationInfoVO.setDistance(stationInfo.getDistance());
            stationInfoVOList.add(stationInfoVO);
        }
        return stationInfoVOList;
    }


    @Autowired
    private GEOTemplateContext geoTemplateContext;
    private StationCacheTemplateRepository getCacheTemplateRepository() {
        StationCacheTemplateRepository repository =
                geoTemplateContext.geoRepositoryTemplate();
        log.debug("获取场站处理模板:{}", repository);
        return repository;
    }


}
